package com.group01.community.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.group01.community.entity.SysUserPost;
import com.group01.community.mapper.SysUserPostMapper;
import com.group01.community.service.SysUserPostService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 用户与岗位关联表 服务实现类
 * </p>
 *
 * @author guoyuan
 * @since 2025-05-01
 */
@Service
public class SysUserPostServiceImpl extends ServiceImpl<SysUserPostMapper, SysUserPost> implements SysUserPostService {


    @Resource
    private SysUserPostMapper sysUserPostMapper;

    @Override
    public boolean handleUserPost(Long userId, Long postId, String flag) {
        if (userId == null){
            throw new RuntimeException("用户信息不能为空");
        }
        if (flag.equals("add") && postId != null){
            return sysUserPostMapper.insert(new SysUserPost().setUserId(userId).setPostId(postId)) > 0;
        }
        if (flag.equals("update")){
            sysUserPostMapper.delete(new QueryWrapper<SysUserPost>().eq("user_id", userId));
        }

        SysUserPost sysUserPost = new SysUserPost();
        sysUserPost.setUserId(userId);
        sysUserPost.setPostId(postId);

        return sysUserPostMapper.insert(sysUserPost) > 0;
    }
}
